package com.lazy.book.service.system.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lazy.book.mapper.system.SysUserRoleMapper;
import com.lazy.book.pojo.system.pojo.po.SysUserRole;
import com.lazy.book.service.system.SysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.lazy.book.pojo.system.pojo.po.table.SysUserRoleTableDef.SYS_USER_ROLE;

/**
 * @author lay
 * @since 2025-09-05
 */
@Service
@RequiredArgsConstructor
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    private final SysUserRoleMapper sysUserRoleMapper;

    @Override
    public void batchInsert(Long userId, List<Long> roleIds) {
        List<SysUserRole> list = roleIds.stream().map(roleId -> {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleId(roleId);
            return sysUserRole;
        }).collect(Collectors.toList());
        sysUserRoleMapper.insertBatch(list);
    }

    @Override
    public List<Long> getRoleIdsByUserId(Long userId) {
        QueryWrapper wrapper = QueryWrapper.create().from(SYS_USER_ROLE).where(SYS_USER_ROLE.USER_ID.eq(userId));
        return list(wrapper).stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
    }

    @Override
    public Set<String> getRoleKeysByUserId(Long userId) {
        return null;
    }

    @Override
    public Set<String> getRoleNamesByUserId(Long userId) {
        return null;
    }

    public void removeByUserId(Long userId) {
        QueryWrapper wrapper = QueryWrapper.create().from(SYS_USER_ROLE).where(SYS_USER_ROLE.USER_ID.eq(userId));
        remove(wrapper);
    }
}